package top.xiaozhaobk.xiaofenshu.note.biz.rpc;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import top.xiaozhaobk.framework.common.response.Response;
import top.xiaozhaobk.xiaofenshu.kv.api.KeyValueFeignApi;
import top.xiaozhaobk.xiaofenshu.kv.dto.req.AddNoteContentReqDTO;
import top.xiaozhaobk.xiaofenshu.kv.dto.req.DeleteNoteContentReqDTO;
import top.xiaozhaobk.xiaofenshu.kv.dto.req.FindNoteContentReqDTO;
import top.xiaozhaobk.xiaofenshu.kv.dto.rsp.FindNoteContentRspDTO;

import java.util.Objects;

@Component
public class KeyValueRpcService {
    @Resource
    private KeyValueFeignApi keyValueFeignApi;

    /**
     * 保存笔记内容
     *
     * @param uuid
     * @param content
     * @return
     */
    public boolean saveNoteContent(String uuid, String content) {
        AddNoteContentReqDTO reqDTO = new AddNoteContentReqDTO ();
        reqDTO.setNoteId(uuid);
        reqDTO.setContent(content);

        Response<?> response = keyValueFeignApi.addNoteContent(reqDTO);
        if (Objects.isNull(response) || !response.isSuccess()) {
            return false;
        }

        return true;
    }

    /**
     * 删除笔记内容
     *
     * @param uuid
     * @return
     */
    public boolean deleteNoteContent(String uuid) {
        DeleteNoteContentReqDTO reqDTO = new DeleteNoteContentReqDTO ();
        reqDTO.setNoteId(uuid);

        Response<?> response = keyValueFeignApi.deleteNoteContent(reqDTO);

        if (Objects.isNull(response) || !response.isSuccess()) {
            return false;
        }

        return true;
    }

    /**
     * 查询笔记内容
     *
     * @param uuid
     * @return
     */
    public String findNoteContent (String uuid) {
        FindNoteContentReqDTO reqDTO = new FindNoteContentReqDTO ();
        reqDTO.setNoteId(uuid);

        Response<FindNoteContentRspDTO> noteContent = keyValueFeignApi.findNoteContent(reqDTO);
        if (Objects.isNull(noteContent) || !noteContent.isSuccess() || Objects.isNull(noteContent.getData())) {
            return null;
        }

        return noteContent.getData().getContent();
    }
}
